package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ShopingCar;
import cn.wolfcode.trip.base.mapper.OrderMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.mapper.ShopingCarMapper;
import cn.wolfcode.trip.base.query.OrderQueryObject;
import cn.wolfcode.trip.base.service.IOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopingCarMapper shopingCarMapper;
    @Autowired
    private ProductMapper productMapper;

   /* @Override
    public PageInfo query(OrderQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Order> list = orderMapper.selectForList(qo);
        return new PageInfo(list);
    }*/

    @Override
    public Order selectOrderById(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrder(Long userId, Long[] productIds,Integer[] numbers,int difference) {
        if (productIds != null && numbers!=null) {
            for (int i =0;i<productIds.length;i++){
                Order order = new Order();
                Product product = new Product();
                product.setId(productIds[i]);
                order.setProduct(product);
                order.setNumber(numbers[i]);
                //增加订单
                orderMapper.insert(order);
                //维护用户与订单关系
                orderMapper.insertRelation(userId,order.getId());
                //不是直接购买时才需要更新购物车
                if(difference!=1){
                    //购物车商品更新
                    ShopingCar car = shopingCarMapper.getNumByUserAndProduct(userId,productIds[i]);
                    Integer number = car.getNumber();
                    number = number - numbers[i];
                    if (number==0){
                        shopingCarMapper.deleteByProductId(userId,productIds[i]);
                    }else {
                        shopingCarMapper.update(userId,productIds[i],number);
                    }
                }
                //更新库存
                Integer amount = productMapper.getAmountByProductId(productIds[i]);
                amount -= numbers[i];
                productMapper.updateAmount(productIds[i],amount);
            }
        }
    }

    @Override
    public PageInfo queryOrders(OrderQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Order> list = orderMapper.selectOrdersForUser(qo);
        if (list != null) {
            for(int i =0;i<list.size();i++){
                if(list.get(i)!=null){
                    if(list.get(i).getProduct()!=null){
                        list.get(i).getProduct().setOrderId(list.get(i).getId());
                    }
                }
            }
        }

        return new PageInfo(list);
    }

    @Override
    public void delete(Long id) {
        orderMapper.deleteByPrimaryKey(id);
        orderMapper.deleteRelation(id);
    }

   /* @Override
    public List<Order> getOrderOrders(Long[] arr) {
        return orderMapper.getOrderOrders(arr);
    }*/

}
